import Vue from 'vue'
import store from '../store'
import Router from 'vue-router'
import Index from '../components/index/Index'
import Login from '../components/login/Login'
import Register from '../components/register/Register'
import positionSelect from '../components/position/positionSelect'
import resume from '../components/resume/Resume'
import hrWork from '../components/hrWork/HrWork'
import addPosition from '../components/position/AddPosition'
import openPosition from '../components/hrWork/handelPosition/openPosition'
import endPosition from '../components/hrWork/handelPosition/endPosition'
import noCheckedPosition from '../components/hrWork/handelPosition/noCheckedPosition'
import UpdatePosition from '../components/position/UpdatePosition'
import PositionDetail from '../components/position/positionDetail'
import CompanyDetail from '../components/company/CompanyDetail'
import myCompany from '../components/hrWork/myCompany'
import Delivery from '../components/userWork/delivery'
import userWork from '../components/userWork/userWork'
import ApplicantOne from '../components/userWork/ApplicantOne'
import ApplicantTwo from '../components/userWork/ApplicantTwo'
import ApplicantThree from '../components/userWork/ApplicantThree'
import ApplicantFour from '../components/userWork/ApplicantFour'
import noResume from '../components/hrWork/handelResume/noResume'
import OneResume from '../components/hrWork/handelResume/OneResume'
import TwoResume from '../components/hrWork/handelResume/TwoResume'
import ThreeResume from '../components/hrWork/handelResume/ThreeResume'
import FourResume from '../components/hrWork/handelResume/FourResume'
import jobCollection from '../components/userWork/jobCollection'
import jobFair from '../components/jobFair/jobFair'
import adminLogin from '../components/adminWork/adminLogin'
import adminWork from '../components/adminWork/adminWork'
import adminCompanyList from '../components/adminWork/adminAccountManage/adminCompanyList'
import adminUserAccount from '../components/adminWork/adminAccountManage/adminUserAccount'
import adminHrAccount from '../components/adminWork/adminAccountManage/adminHrAccount'
import adminPosition from '../components/adminWork/adminPositionManage/adminPosition'
import adminJobFair from '../components/adminWork/adminJobFair/AdminJobFair'
import adminJobFairAdd from '../components/adminWork/adminJobFair/AdminJobFairAdd'
import adminJobFairUpdate from '../components/adminWork/adminJobFair/AdminJobFairUpdate'
import NewsList from '../components/adminWork/adminNews/NewsList'
import addNews from '../components/adminWork/adminNews/AddNews'
import UpdateNews from '../components/adminWork/adminNews/UpdateNews'
import UserNewsList from '../components/News/UserNewsList'
import UserNewsDetail from '../components/News/UserNewsDetail'

import wangEditor from '../components/wangEditor/wangEditor'
Vue.use(Router)
//创建路由对象并配置路由
const router = new Router({
  mode: 'hash',
  routes: [
    {
      path: '/wangEditor',
      component: wangEditor
    },
    {
      path: '/',
      component: Index,
      // meta: {
      //   requireAdmin: false,//false为不登陆也可看，true为登录才可看
      //   requireHr: false,//false为不登陆也可看，true为登录才可看
      //   requireUser: false,//false为不登陆也可看，true为登录才可看
      // },
    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/register',
      component: Register
    },
    {
      path: '/positionSelect',
      component: positionSelect,
      meta: {
        title: ' ',
        keepAlive: true,//此组件需要被缓存
      }
    },
    {
      path: '/resume',
      component: resume,
      meta: {
        requireUser: true//false为不登陆也可看，true为登录才可看
      },
    },
    {
      path: '/positionDetail/:positionId',
      component: PositionDetail
    },
    {
      path: '/jobCollection',
      component: jobCollection,
      meta: {
        requireUser: true//false为不登陆也可看，true为登录才可看
      },
    },
    {
      path: '/CompanyDetail',
      component: CompanyDetail,
    },
    {
      path: '/adminLogin',
      component:adminLogin
    },
    {
      path: '/jobFair',
      component:jobFair
    },
    {
      path: '/UserNewsList',
      component: UserNewsList
    },
    {
      path: '/UserNewsDetail',
      component:UserNewsDetail
    },
    {
      path:'/userWork',
      component: userWork,
      meta: {
        requireUser: true,//false为不登陆也可看，true为登录才可看
      },
      children:[
        {
          path:'delivery',
          component: Delivery,
          meta: {
            requireUser: true,//false为不登陆也可看，true为登录才可看
          },
        },
        {
          path:'ApplicantOne',
          component: ApplicantOne,
          meta: {
            requireUser: true,//false为不登陆也可看，true为登录才可看
          },
        },
        {
          path:'ApplicantTwo',
          component: ApplicantTwo,
          meta: {
            requireUser: true,//false为不登陆也可看，true为登录才可看
          },
        },
        {
          path:'ApplicantThree',
          component: ApplicantThree,
          meta: {
            requireUser: true,//false为不登陆也可看，true为登录才可看
          },
        },
        {
          path:'ApplicantFour',
          component: ApplicantFour,
          meta: {
            requireUser: true,//false为不登陆也可看，true为登录才可看
          },
        }
      ]
    },
    {
      path:'/hrWork',
      component: hrWork,
      meta: {
        requireHr: true,//false为不登陆也可看，true为登录才可看
      },
      children:[
        {
          path: 'addPosition',
          component: addPosition,
          meta: {
            requireHr: true,//false为不登陆也可看，true为登录才可看
          },
        },
        {
          path:'UpdatePosition/:positionId',
          component: UpdatePosition,
          meta: {
            requireHr: true,//false为不登陆也可看，true为登录才可看
          },
        },
        {
          path: 'openPosition',
          component: openPosition,
          meta: {
            requireHr: true,//false为不登陆也可看，true为登录才可看
          },
        },
        {
          path: 'endPosition',
          component: endPosition,
          meta: {
            requireHr: true,//false为不登陆也可看，true为登录才可看
          },
        },
        {
          path: 'noCheckedPosition',
          component: noCheckedPosition,
          meta: {
            requireHr: true,//false为不登陆也可看，true为登录才可看
          },
        },
        {
          path: 'myCompany',
          component: myCompany,
          meta: {
            requireHr: true,//false为不登陆也可看，true为登录才可看
          },
        },
        {
          path: 'noResume',
          component: noResume,
          meta: {
            requireHr: true,//false为不登陆也可看，true为登录才可看
          },
        },
        {
          path: 'OneResume',
          component: OneResume,
          meta: {
            requireHr: true,//false为不登陆也可看，true为登录才可看
          },
        },
        {
          path: 'TwoResume',
          component: TwoResume,
          meta: {
            requireHr: true,//false为不登陆也可看，true为登录才可看
          },
        },
        {
          path: 'ThreeResume',
          component: ThreeResume,
          meta: {
            requireHr: true,//false为不登陆也可看，true为登录才可看
          },
        },
        {
          path: 'FourResume',
          component: FourResume,
          meta: {
            requireHr: true,//false为不登陆也可看，true为登录才可看
          },
        },
      ]
    },
    {
      "path": '/adminWork',
      component:adminWork,
      meta: {
        requireAdmin: true,//false为不登陆也可看，true为登录才可看
      },
      children:[
        {
          path: 'adminCompanyList',
          component: adminCompanyList,
          meta: {
            requireAdmin: true,//false为不登陆也可看，true为登录才可看
          },
        },
        {
          path: 'adminUserAccount',
          component: adminUserAccount,
          meta: {
            requireAdmin: true,//false为不登陆也可看，true为登录才可看
          },
        },
        {
          path: 'adminHrAccount',
          component: adminHrAccount,
          meta: {
            requireAdmin: true,//false为不登陆也可看，true为登录才可看
            requireHr: false,//false为不登陆也可看，true为登录才可看
            requireUser: false,//false为不登陆也可看，true为登录才可看
          },
        },
        {
          path: 'adminPosition',
          component: adminPosition,
          meta: {
            requireAdmin: true,//false为不登陆也可看，true为登录才可看
          },
        },
        {
          path: 'adminJobFair',
          component: adminJobFair,
          meta: {
            requireAdmin: true,//false为不登陆也可看，true为登录才可看
          },
        },
        {
          path: 'adminJobFairAdd',
          component: adminJobFairAdd,
          meta: {
            requireAdmin: true,//false为不登陆也可看，true为登录才可看
          },
        },
        {
          path: 'adminJobFairUpdate',
          component: adminJobFairUpdate,
          meta: {
            requireAdmin: true,//false为不登陆也可看，true为登录才可看
          },
        },
        {
          path: 'NewsList',
          component:NewsList,
          meta: {
            requireAdmin: true,//false为不登陆也可看，true为登录才可看
          },
        },
        {
          path: 'addNews',
          component: addNews,
          meta: {
            requireAdmin: true,//false为不登陆也可看，true为登录才可看
          },
        },
        {
          path: 'UpdateNews',
          component:UpdateNews,
          meta: {
            requireAdmin: true,//false为不登陆也可看，true为登录才可看
          },
        },

      ]
    }
  ]
})

// 配置路由守卫
router.beforeEach((to,from,next)=>{
  console.log("进入路由守卫");
  console.log(to.meta.requireUser && to.meta.requireHr && to.meta.requireAdmin);
  // if (sessionStorage.getItem('store')) {
  //   store.replaceState(Object.assign({}, store.state, JSON.parse(sessionStorage.getItem('store'))));
  // }
  // //在页面刷新时将vuex里的信息保存到sessionStorage里
  // window.addEventListener('beforeunload', () => {
  //   sessionStorage.setItem('store', JSON.stringify(store.state));
  // });
  try{
    if (to.meta.requireUser || to.meta.requireHr || to.meta.requireAdmin) {
      console.log("进入路由守卫1");
      if (to.meta.requireUser) { //需要user权限
        let user = store.getters.user?store.getters.user:window.sessionStorage.getItem("user");
        // let user = store.getters.user
        console.log(user);
        console.log(JSON.stringify(user));
        if (JSON.stringify(user) === '{}') {//没有登录则跳转到登录界面
          console.log(JSON.stringify(user));
          next('/login')
        } else { //已经登录
          next()
        }
      }
      if (to.meta.requireHr) { //需要hr权限
        let hr = store.getters.hr?store.getters.hr:window.sessionStorage.getItem("hr");
        if (JSON.stringify(hr) === '{}') {//没有登录则跳转到登录界面
          next('/login')
        } else { //已经登录
          next()
        }
      }
      if (to.meta.requireAdmin) { //需要admin权限
        // let admin = store.getters.admin;
        let admin = store.getters.admin?store.getters.admin:window.sessionStorage.getItem("admin");
        console.log(JSON.stringify(admin));
        if (JSON.stringify(admin) === '{}') {//没有登录则跳转到登录界面
          next('/adminLogin')
        } else { //已经登录
          next()
        }
      }
    } else {
      console.log("无需权限");
      next();
    }
  }
  catch {
    next();
  }
})


export default router